<?php
namespace Home\Controller;
use Think\Controller;
class IndexController extends Controller {
  // 后台登录页
    public function index(){
        if(isset($_SESSION['admin'])){
            $url = 'http://localhost/person/index.php/Home/Index/manage';
            header("location: $url");
        }else{
            $this->display();
        }
    }
    // 登录函数
    public function login($name, $password){
        // $name = $_POST['name'];
        // $password = $_POST['password'];
        $admin = M('admin');
        $data = $admin->where("name='$name' AND password='$password'")->find();
        if($data){
            $_SESSION['admin']=$name;
            $result = array(
                'code' => '0',
                'ext' => '登录成功',
                'adminName' => $_SESSION['admin']
            );
            echo json_encode($result);
        }else{
            $result = array(
                'code' => '1',
                'ext' => '用户名或密码错误'
            );
            echo json_encode($result);
        }
    }

     public function personDelete($id){
        $person = D('person');
        $data['state'] = 1;        
        //$success = $project->where("id='$id'")->delete();
        $success = $person->where("id='$id'")->delete($data);
        if($success){
            $result = array(
                'code'=> '0',
                'ext'=> 'success'
            );
            echo json_encode($result);
        }else {
            $result = array(
                'code'=> '1',
                'ext'=> 'fail'
            );
            echo json_encode($result);
        }
    }




     // 管理页面首页
    public function manage(){
        if(!isset($_SESSION['admin']) || !$_SESSION['admin']){
            $url = 'http://localhost/person/index.php/Home/Index';
            header("location: $url");
        }else{

            $person = D("person");
            $position = D("position");
            $sector = D("sector");
            $personList = $person->select();
            for($i=0; $i < count($personList); $i++){
                $positionId = $personList[$i]['position_id'];              
                $name = $position->where("id='$positionId'")->field('name')->select();
                $personList[$i]['position'] = $name[0]['name'];

                $sectorId = $personList[$i]['sector_id'];              
                $sectorname = $sector->where("id='$sectorId'")->field('name')->select();
                $personList[$i]['sector'] = $sectorname[0]['name'];


                if($personList[$i]['sex']){
                    $personList[$i]['sexname'] = "女";
                }else{
                    $personList[$i]['sexname'] = "男";
                }
            }

            $this->assign('list',$personList);          
            $this->display();
        }
    }
    //退出
    public function doLogout(){
        $_SESSION = array();
        if(isset($_COOKIE[session_name()])){
            setcookie(session_name(),'',time()-1,'/');
        }
        session_destroy();
        $this->redirect('Index/index');
    }
}